function HotPoint(activity) {
    this.activity = activity;
    this.boxStyle = {
        stroke: "#39F",
        "stroke-dasharray": "-"
    };
    this.boxPoint = {
        fill: "#39F",
        stroke: "none"
    };
    var bbox = activity.view.getBBox();
    this.box = activity.context.paper.rect(bbox.x, bbox.y, bbox.width, bbox.height).attr(this.boxStyle);
    // 1 top left
    this.topLeft = activity.context.paper.rect(bbox.x - 3, bbox.y - 3, 6, 6).attr(this.boxPoint);
    this.bindData(this.topLeft);
    
    this.topLeft.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    // 2 top middle
    this.topMiddle = activity.context.paper.rect(bbox.x - 3 + bbox.width / 2, bbox.y - 3, 6, 6).attr(this.boxPoint);
    this.bindData(this.topMiddle);
    
    this.topMiddle.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    // 3 top right
    this.topRight = activity.context.paper.rect(bbox.x - 3 + bbox.width, bbox.y - 3, 6, 6).attr(this.boxPoint);
    this.bindData(this.topRight);
    
    this.topRight.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    
    // 4 middle left
    this.middleLeft = activity.context.paper.rect(bbox.x - 3, bbox.y - 3 + bbox.height / 2, 6, 6).attr(this.boxPoint);
    this.bindData(this.middleLeft);
    
    this.middleLeft.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    
    // 5 middle right
    this.middleRight = activity.context.paper.rect(bbox.x - 3 + bbox.width, bbox.y - 3 + bbox.height / 2, 6, 6).attr(this.boxPoint);
    this.bindData(this.middleRight);
    
    this.middleRight.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    
    // 6 bottom left
    this.bottomLeft = activity.context.paper.rect(bbox.x - 3, bbox.y - 3 + bbox.height, 6, 6).attr(this.boxPoint);
    this.bindData(this.bottomLeft);
    
    this.bottomLeft.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    
    // 7 bottom middle
    this.bottomMiddle = activity.context.paper.rect(bbox.x - 3 + bbox.width / 2, bbox.y - 3 + bbox.height, 6, 6).attr(this.boxPoint);
    this.bindData(this.bottomMiddle);
    
    this.bottomMiddle.mousedown(function(event) {
        HotPoint_CreateTransition(this);
    });
    
    // 8 bottom right
    this.bottomRight = activity.context.paper.rect(bbox.x - 3 + bbox.width, bbox.y - 3 + bbox.height, 6, 6).attr(this.boxPoint);
    this.bindData(this.bottomRight);
    
    this.bottomRight.mousedown(function(event) {
    
        HotPoint_CreateTransition(this);
    });
    
}

HotPoint.prototype.bindData = function(box) {
    box.data("_data", this);
};

function HotPoint_CreateTransition(hotPoint) {
    var activity = hotPoint.data("_data").activity;
    
    if ((activity.context.command == "line") && (activity.context.commandObject == null)) {
    
        if (activity.clazz == "E") {
            return false;
        }
        
        if (context.getTransitionOfActivity(activity).startActivityList.length > 0 && (activity.clazz == "R" || activity.clazz == "J")) {
            return false;
        }
        
        activity.context.commandObject = new Transition(activity, null, hotPoint);
    } else {
        HotPoint_ConfirmTransition(activity.hotPoint.middleLeft);
        activity.context.commandObject = null;
    }
}

function HotPoint_ConfirmTransition(hotPoint) {
    var activity = hotPoint.data("_data").activity;
    if (activity.context.command == "line" && activity.context.commandObject != null &&
    activity.context.commandObject instanceof Transition &&
    activity.context.commandObject.startActivity.sid != activity.sid &&
    activity.clazz != "R") {
        activity.context.commandObject.endActivity = activity;
        activity.context.commandObject.endBox = hotPoint;
        //activity.context.commandObject.update();
        // 计算路由坐标
        var path = activity.context.commandObject.view.attrs.path;
        var ex = activity.hotPoint.topMiddle.attrs.x;
        var ey = activity.hotPoint.middleLeft.attrs.y;
        var result = compareCoordinate({
            x: path[path.length - 2][1],
            y: path[path.length - 2][2]
        }, {
            x: ex,
            y: ey
        }, activity.context.commandObject);
        if (activity.context.commandObject.view.attrs.path.length == 2) {
            activity.context.commandObject.updateStartXY(result.start.x, result.start.y);
        }
        activity.context.commandObject.updateEndXY(result.end.x, result.end.y);
        activity.context.addTransition(activity.context.commandObject);
        
    } else if (activity.context.commandObject instanceof Transition) {
        activity.context.commandObject.remove();
    }
    
};
HotPoint.prototype.hide = function() {
    this.box.hide();
    this.topLeft.hide();
    this.topMiddle.hide();
    this.topRight.hide();
    this.middleLeft.hide();
    this.middleRight.hide();
    this.bottomLeft.hide();
    this.bottomMiddle.hide();
    this.bottomRight.hide();
};
HotPoint.prototype.show = function() {
    this.box.show();
    this.topLeft.show();
    this.topRight.show();
    this.topMiddle.show();
    this.middleLeft.show();
    this.middleRight.show();
    this.bottomLeft.show();
    this.bottomMiddle.show();
    this.bottomRight.show();
};
HotPoint.prototype.update = function() {
    var bbox = this.activity.view.getBBox();
    this.box.attr({
        x: bbox.x,
        y: bbox.y,
        width: bbox.width,
        height: bbox.height
    });
    // 1 top left
    this.topLeft.attr({
        x: bbox.x - 3,
        y: bbox.y - 3,
        width: 6,
        height: 6
    });
    // 2 top middle
    this.topMiddle({
        x: bbox.x - 3 + bbox.width / 2,
        y: bbox.y - 3,
        width: 6,
        height: 6
    });
    // 3 top right
    this.topRight.attr({
        x: bbox.x - 3 + bbox.width,
        y: bbox.y - 3,
        width: 6,
        height: 6
    });
    // 4 middle left
    this.middleLeft.attr({
        x: bbox.x - 3,
        y: bbox.y - 3 + bbox.height / 2,
        width: 6,
        height: 6
    });
    // 5 middle right
    this.middleRight.attr({
        x: bbox.x - 3 + bbox.width,
        y: bbox.y - 3 + bbox.height / 2,
        width: 6,
        height: 6
    });
    // 6 bottom left
    this.bottomLeft.attr({
        x: bbox.x - 3,
        y: bbox.y - 3 + bbox.height,
        width: 6,
        heigth: 6
    });
    // 7 bottom middle
    this.bottomMiddle.attr({
        x: bbox.x - 3 + bbox.width / 2,
        y: bbox.y - 3 + bbox.height,
        width: 6,
        height: 6
    });
    // 8 bottom right
    this.bottomRight.attr({
        x: bbox.x - 3 + bbox.width,
        y: bbox.y - 3 + bbox.height,
        width: 6,
        height: 6
    }).attr(this.boxPoint);
};

HotPoint.prototype.updateMove = function(dx, dy) {
    var bbox = this.activity.view.getBBox();
    this.box.attr({
        x: bbox.x + dx,
        y: bbox.y + dy,
        width: bbox.width,
        height: bbox.height
    });
    // 1 top left
    this.topLeft.attr({
        x: bbox.x - 3 + dx,
        y: bbox.y - 3 + dy,
        width: 6,
        height: 6
    });
    // 2 top middle
    this.topMiddle.attr({
        x: bbox.x - 3 + bbox.width / 2 + dx,
        y: bbox.y - 3 + dy,
        width: 6,
        height: 6
    });
    // 3 top right
    this.topRight.attr({
        x: bbox.x - 3 + bbox.width + dx,
        y: bbox.y - 3 + dy,
        width: 6,
        height: 6
    });
    // 4 middle left
    this.middleLeft.attr({
        x: bbox.x - 3 + dx,
        y: bbox.y - 3 + bbox.height / 2 + dy,
        width: 6,
        height: 6
    });
    // 5 middle right
    this.middleRight.attr({
        x: bbox.x - 3 + bbox.width + dx,
        y: bbox.y - 3 + bbox.height / 2 + dy,
        width: 6,
        height: 6
    });
    // 6 bottom left
    this.bottomLeft.attr({
        x: bbox.x - 3 + dx,
        y: bbox.y - 3 + bbox.height + dy,
        width: 6,
        heigth: 6
    });
    // 7 bottom middle
    this.bottomMiddle.attr({
        x: bbox.x - 3 + bbox.width / 2 + dx,
        y: bbox.y - 3 + bbox.height + dy,
        width: 6,
        height: 6
    });
    // 8 bottom right
    this.bottomRight.attr({
        x: bbox.x - 3 + bbox.width + dx,
        y: bbox.y - 3 + bbox.height + dy,
        width: 6,
        height: 6
    });
};
